<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    const empValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (+value < 0 || value % 1 !== 0) {
        callback(new Error('人数需填写正整数'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: 'market_project',
      isCommon: false,
      formItems: [
        {
          tag: 'input',
          itemAttrs: {
            label: '销售收入',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'sale_income', value: '', placeholder: '请输入销售收入' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '资产总额',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'assets_funds', value: '', placeholder: '请输入资产总额' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '从业人数',
            rules: [
              // { required: true, message: '请输入从业人数', trigger: 'blur' },
              { validator: isNumberValidator, trigger: 'blur' },
              { validator: empValidator, trigger: 'blur' },
            ],
          },
          attrs: { key: 'emp_total', value: '', placeholder: '请输入从业人数' },
          tagSlots: [{ name: 'append', slot: '人' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '上年度纳税额',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'tax_funds', value: '', placeholder: '请输入上年度纳税额' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'textarea',
          itemAttrs: { label: '备注' },
          attrs: {
            key: 'remark',
            value: '',
            placeholder: '备注',
            autosize: { minRows: 3 },
          },
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId', 'org']),
  },
  watch: {
    visible(val) {
      this.innerVisible = val
      this.isCommon = +this.formData.customer_type === 1
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      Object.keys(model).forEach(item => {
        model[item] = model[item] || null
      })
      model.main_business = +model.is_ind12 === 1 ? model.main_business : ''
      // model.register_no = this.isCommon ? model.register_no : ''
      model.corp_name = this.isCommon ? model.corp_name : model.customer_name
      model.customer_type = this.isCommon ? 1 : 2
      model.service_name = this.org.orgName
      model.service_id = this.clientId
      if (!this.tableId) {
        model.is_history = 1
      }
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
